package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.TitleMenu;
import org.dromara.shopping.service.ITitleMenuService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 小程序首页菜单入口
 *
 * @author Mr.Zhou
 * @date 2025-01-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/menu")
public class TitleMenuController extends BaseController {

    private final ITitleMenuService titleMenuService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    @Value("${ruoyi.profile}")
    private String uploadPath;


    /**
     * 查询小程序首页菜单入口列表
     */
    @SaCheckPermission("shopping:menu:list")
    @GetMapping("/list")
    public TableDataInfo<TitleMenu> list(TitleMenu titleMenu, PageQuery pageQuery) {
        QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
        if(titleMenu.getMenuType()!= null){
            qw.eq("menu_type", titleMenu.getMenuType());
        }
        if(titleMenu.getCategoryType()!= null){
            qw.eq("category_type", titleMenu.getCategoryType());
        }
        qw.orderByDesc("id");

        TableDataInfo<TitleMenu> td = PageUtils.buildDataInfo(titleMenuService.page(PageUtils.buildPage(),qw));
        List<TitleMenu> list = td.getRows();
        for (TitleMenu menu : list){
            if(StrUtil.isNotEmpty(menu.getLinkLogo())){
                menu.setLinkLogoPath(imgUrl + menu.getLinkLogo());
            }
        }
        return td;
    }

    /**
     * 导出小程序首页菜单入口列表
     */
    @SaCheckPermission("shopping:menu:export")
    @Log(title = "小程序首页菜单入口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TitleMenu titleMenu, HttpServletResponse response) {
        List<TitleMenu> list = titleMenuService.list();
        ExcelUtil.exportExcel(list, "小程序首页菜单入口", TitleMenu.class, response);
    }

    /**
     * 获取小程序首页菜单入口详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:menu:query")
    @GetMapping("/{id}")
    public R<TitleMenu> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        TitleMenu menu = titleMenuService.getById(id);
        if(menu != null){
            if(StrUtil.isNotEmpty(menu.getLinkLogo())){
                menu.setLinkLogoPath(imgUrl + menu.getLinkLogo());
            }
        }
        return R.ok(menu);
    }

    /**
     * 新增小程序首页菜单入口
     */
    @SaCheckPermission("shopping:menu:add")
    @Log(title = "小程序首页菜单入口", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TitleMenu titleMenu) {
        return toAjax(titleMenuService.save(titleMenu));
    }

    /**
     * 修改小程序首页菜单入口
     */
    @SaCheckPermission("shopping:menu:edit")
    @Log(title = "小程序首页菜单入口", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TitleMenu titleMenu) {
        return toAjax(titleMenuService.updateById(titleMenu));
    }

    /**
     * 删除小程序首页菜单入口
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:menu:remove")
    @Log(title = "小程序首页菜单入口", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(titleMenuService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
